CC = gcc
MAKE = make

CFLAGS = -Wall -Wextra
CPPFLAGS = -I$(HEADERS_DIR) $(addprefix -I, $(ADDITIONAL_HEADERS_DIR))
LDFLAGS = $(addprefix -L, $(LIBS_DIR))

BASE_DIR = ./
HEADERS_DIR = $(BASE_DIR)headers/
ADDITIONAL_HEADERS_DIR = $(BASE_DIR)lib1/headers/ $(BASE_DIR)lib2/headers/ $(BASE_DIR)lib3/headers/
SOURCES_DIR = $(BASE_DIR)src/
OBJECTS_DIR = $(BASE_DIR)objs/
LIBS_DIR = $(BASE_DIR)lib1/ $(BASE_DIR)lib2/ $(BASE_DIR)lib3/
DIRS = lib1 lib2 lib3
BUILDDIRS = $(DIRS:%=build-%)
CLEANDIRS = $(DIRS:%=clean-%)

HEADERS = 
SOURCES = main.c
OBJECTS = main.o
LIBS = -llib1 -llib2 -llib3

HEADERS_LIST = $(addprefix $(HEADERS_DIR), $(HEADERS))
SOURCES_LIST = $(addprefix $(SOURCES_DIR), $(SOURCES))
OBJECTS_LIST = $(addprefix $(OBJECTS_DIR), $(OBJECTS))

.PHONY: all clean

all: $(OBJECTS_LIST) $(BUILDDIRS)
	$(CC) $(LDFLAGS) $(OBJECTS_LIST) -o main $(LIBS)
	
$(OBJECTS_DIR)%.o: $(SOURCES_DIR)%.c $(HEADERS_LIST)
	$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

$(BUILDDIRS):
	$(MAKE) -C $(@:build-%=%)

clean: $(CLEANDIRS)
	rm main objs/*

$(CLEANDIRS):
	$(MAKE) -C $(@:clean-%=%) clean
